package com.ihisun.tmhcms.core.service.openingrecordservice.impl;

import com.ihisun.tmhcms.core.dao.edgiftbagdao.VipPackageDao;
import com.ihisun.tmhcms.core.dao.openingrecorddao.OpeningRecordDao;
import com.ihisun.tmhcms.core.dao.userdao.UserAddressDao;
import com.ihisun.tmhcms.core.dao.userdao.UserDao;
import com.ihisun.tmhcms.core.dto.searchvipnumberdto.SearchVipNumberDto;
import com.ihisun.tmhcms.core.dto.searchvipnumberdto.VipNumberDeliver;
import com.ihisun.tmhcms.core.dto.userdto.UserAddressDto;
import com.ihisun.tmhcms.core.service.openingrecordservice.OpeningRecordService;
import com.ihisun.tmhcms.core.vo.OpeningRecordVo;
import com.ihisun.tmhcms.core.vo.UserVo;
import com.ihisun.tmhcms.core.vo.VipPackageVo;
import com.ihisun.tmhcms.utils.DateUtils;
import com.ihisun.tmhcms.utils.PageBean;
import com.ihisun.tmhcms.utils.ResponseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.math.BigInteger;
import java.util.List;


@Service
public class OpeningRecordServiceImpl implements OpeningRecordService {
    @PersistenceContext
    EntityManager entityManager;
    @Resource
    UserDao userDao;
    @Resource
    VipPackageDao vipPackageDao;
    @Resource
    OpeningRecordDao openingRecordDao;
    @Resource
    UserAddressDao userAddressDao;

    @Override
    public ResponseVo findByOpeningRecord(SearchVipNumberDto searchVipNumberDto) {

        StringBuilder sql = new StringBuilder("select * from v_vip_number where order_state=1");
        if (searchVipNumberDto.getPackageName() != null && !"".equals(searchVipNumberDto.getPackageName())) {
            sql.append(" and package_name like concat('%','" + searchVipNumberDto.getPackageName() + "','%')");
        }
        if (searchVipNumberDto.getDeliverGoodsOrConsumption() != null && !"".equals(searchVipNumberDto.getDeliverGoodsOrConsumption())) {
            sql.append(" and deliver_goods_or_consumption like concat('%','" + searchVipNumberDto.getDeliverGoodsOrConsumption() + "','%')");
        }
        if (searchVipNumberDto.getGiftBagType() != null && !"".equals(searchVipNumberDto.getGiftBagType())) {
            sql.append(" and gift_bag_type like concat('%','" + searchVipNumberDto.getGiftBagType() + "','%')");
        }
        if (!"".equals(searchVipNumberDto.getFrontDate()) && !"".equals(searchVipNumberDto.getAfterDate())) {
            sql.append("  and opening_time > " + searchVipNumberDto.getFrontDate() + " and opening_time < " + searchVipNumberDto.getAfterDate() + "");
        }
        if ("".equals(searchVipNumberDto.getFrontDate()) && !"".equals(searchVipNumberDto.getAfterDate())) {
            sql.append(" and opening_time < " + searchVipNumberDto.getAfterDate() + "");
        }
        if (!"".equals(searchVipNumberDto.getFrontDate()) && "".equals(searchVipNumberDto.getAfterDate())) {
            sql.append(" and opening_time > " + searchVipNumberDto.getFrontDate() + "");
        }
        if (searchVipNumberDto.getNickName() != null && !"".equals(searchVipNumberDto.getNickName())) {
            sql.append(" and uuid in(select uuid from u_user where nick_name like concat('%','" + searchVipNumberDto.getNickName() + "','%'))");
        }
        if ("".equals(searchVipNumberDto.getNickName()) || searchVipNumberDto.getNickName() == null) {
            sql.append(" and uuid in(select uuid from u_user)");
        }
        sql.append(" limit " + (searchVipNumberDto.getPageNum() - 1) * 10 + "," + searchVipNumberDto.getPageSize() + "");

        List<OpeningRecordVo> resultList = entityManager.createNativeQuery(sql.toString(), OpeningRecordVo.class).getResultList();
        if (resultList != null && !resultList.isEmpty()) {
            for (int i = 0; i < resultList.size(); i++) {
                UserVo byUuid = userDao.findByUuid(resultList.get(i).getUuid());
                resultList.get(i).setUserVo(byUuid);
                VipPackageVo byPackageId = vipPackageDao.findByPackageId(resultList.get(i).getPackageId());
                if (byPackageId != null) {
                    if (byPackageId.getGiftBagType().equals("2")) {
                        if (resultList.get(i).getState().equals("1")) {
                            resultList.get(i).setState("已使用");
                        }
                    }
                    if (byPackageId.getGiftBagType().equals("1")) {
                        if (resultList.get(i).getState().equals("1")) {
                            resultList.get(i).setState("已发货");
                        }
                    }
                    if (byPackageId.getGiftBagType().equals("2")) {
                        if (resultList.get(i).getState().equals("2")) {
                            resultList.get(i).setState("未使用");
                        }
                    }
                    if (byPackageId.getGiftBagType().equals("1")) {
                        if (resultList.get(i).getState().equals("2")) {
                            resultList.get(i).setState("未发货");
                        }
                    }
                    if (byPackageId.getGiftBagType().equals("1")) {
                        resultList.get(i).setGiftBagType("实物");
                    }
                    if (byPackageId.getGiftBagType().equals("2")) {
                        resultList.get(i).setGiftBagType("项目");
                    }
                    if (byPackageId.getPostage().equals("1")) {
                        resultList.get(i).setPostage("包邮");
                    }
                    if (byPackageId.getPostage().equals("2")) {
                        resultList.get(i).setPostage(byPackageId.getSpecificPostage());
                    }
                }
            }
        }


        StringBuilder countSql = new StringBuilder("select count(*) from v_vip_number where order_state=1");
        if (searchVipNumberDto.getPackageName() != null && !"".equals(searchVipNumberDto.getPackageName())) {
            countSql.append(" and package_name like concat('%','" + searchVipNumberDto.getPackageName() + "','%')");
        }
        if (searchVipNumberDto.getDeliverGoodsOrConsumption() != null && !"".equals(searchVipNumberDto.getDeliverGoodsOrConsumption())) {
            countSql.append(" and deliver_goods_or_consumption like concat('%','" + searchVipNumberDto.getDeliverGoodsOrConsumption() + "','%')");
        }
        if (searchVipNumberDto.getGiftBagType() != null && !"".equals(searchVipNumberDto.getGiftBagType())) {
            countSql.append(" and gift_bag_type like concat('%','" + searchVipNumberDto.getGiftBagType() + "','%')");
        }
        if (!"".equals(searchVipNumberDto.getFrontDate()) && !"".equals(searchVipNumberDto.getAfterDate())) {
            countSql.append("  and opening_time > " + searchVipNumberDto.getFrontDate() + " and opening_time < " + searchVipNumberDto.getAfterDate() + "");
        }
        if ("".equals(searchVipNumberDto.getFrontDate()) && !"".equals(searchVipNumberDto.getAfterDate())) {
            countSql.append(" and opening_time < " + searchVipNumberDto.getAfterDate() + "");
        }
        if (!"".equals(searchVipNumberDto.getFrontDate()) && "".equals(searchVipNumberDto.getAfterDate())) {
            countSql.append(" and opening_time > " + searchVipNumberDto.getFrontDate() + "");
        }
        if (searchVipNumberDto.getNickName() != null && !"".equals(searchVipNumberDto.getNickName())) {
            countSql.append(" and uuid in(select uuid from u_user where nick_name like concat('%','" + searchVipNumberDto.getNickName() + "','%'))");
        }
        if ("".equals(searchVipNumberDto.getNickName()) || searchVipNumberDto.getNickName() == null) {
            countSql.append(" and uuid in(select uuid from u_user)");
        }
        BigInteger singleResult = (BigInteger) entityManager.createNativeQuery(countSql.toString()).getSingleResult();

        PageBean pageBean = new PageBean(searchVipNumberDto.getPageNum(), singleResult.intValue(), searchVipNumberDto.getPageSize(), resultList);


        return ResponseVo.ok(pageBean);
    }

    @Override
    public ResponseVo orderInfo(String orderId) {
        OpeningRecordVo byOrderId = openingRecordDao.findByOrderId(orderId);
        if (byOrderId != null) {
            UserVo byUuid = userDao.findByUuid(byOrderId.getUuid());
            if (byUuid != null) {
                byOrderId.setUserVo(byUuid);
            }
            UserAddressDto byUuid1 = userAddressDao.findByUuid(byOrderId.getUuid());
            if (byOrderId.getGiftBagType().equals("1")) {
                byOrderId.setUserAddressDto(byUuid1);
            }
            VipPackageVo byPackageId = vipPackageDao.findByPackageId(byOrderId.getPackageId());
            byOrderId.setVipPackageVo(byPackageId);
        }
        return ResponseVo.ok(byOrderId);
    }

    @Override
    public ResponseVo deliverGoods(VipNumberDeliver vipNumberDeliver) {
        OpeningRecordVo byOrderId = openingRecordDao.findByOrderId(vipNumberDeliver.getOrderId());
        byOrderId.setState("1");
        byOrderId.setCourierNumber(vipNumberDeliver.getOddNumbers());
        byOrderId.setUpdateTime(DateUtils.getCurrentTime());
        openingRecordDao.save(byOrderId);
        return ResponseVo.ok();
    }
}
